<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/18
 * Time: 14:31
 */
namespace app\index\model;
use app\console\model\MemberMessage;
use think\Model;
use think\Request;
use think\Validate;
use think\Loader;
use think\Db;

class Member extends Model
{

    protected static function init()
    {
        self::event('after_write', function ($data) {
            $d = Db::name('message')->select();
                $member_list = Db::name('member')->select();
                $insertAll = [];
                if(!empty($d)){
                        foreach ($d as $k1 =>  $v1){
                            $insertAll[] = [
                                'member_id' => $data->id,
                                'title' => $v1['title'],
                                'content' => $v1['content'],
                                'create_time' => $v1['create_time'],
                                'message_id' => $v1['id']
                            ];
                        }

                    (new MemberMessage())->insertAll($insertAll);
                }
        });

    }

    public function reg()
    {
        $param = Request::instance()->param();
        $result = $this->validate(true)->save($param);
        if (false === $result) {
            // 验证失败 输出错误信息
            dump($this->getError());
        }
    }

    public function reg1(){
        $validate = Loader::validate('Member');
        $param = Request::instance()->param();
        if (!$validate->check($param)) return ['code' => 400, 'message' => $validate->getError()];
    }
}